<?php
/*
 *  Jiba: A small collection of classes to make php easier.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * timestamp.class.php, Jiba
 * 
 * Keeps the time, it allows you to set and print
 * the time whatever the users local method is. 
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package jiba
 */

class Timestamp
{
  private $_unixtime;
  
  public function __construct()
  {
    $this->_unixtime = time();
  }
  
  public function setTime( $M, $d, $y, $h=0, $m=0, $s=0 )
  {
    $this->_unixtime = mktime( intval($h), intval($m), intval($s), intval($M), intval($d), intval($y) );
  }
  
  /**
   * Use a mysqltimestamp as input
   */
  public function setMysqlTimestamp( $stamp )
  {
    //seperate date and time
    list( $date, $time ) = explode( ' ', $stamp );
    
    //split the time
    list( $hour, $minutes, $seconds ) = explode( ':', $time );
    
    //split the date
    list( $year, $month, $day ) = explode( '-', $date );
    
    $this->settime( $month, $day, $year, $hour, $minutes, $seconds );
  }
  
  public function setHour( $hour )
  {
    $hour -= date('G',$this->_unixtime);
  
    $this->_unixtime = strtotime( "+$hour hours", $this->_unixtime );
    return $this; 
  }
  
  public function setMinutes( $minutes )
  {
    $minutes -= date('i',$this->_unixtime);
    
    $this->_unixtime = strtotime( "+$minutes minutes", $this->_unixtime );
    return $this; 
  }
  
  public function setMonth( $month )
  {
    $month -= date( 'n', $this->_unixtime );
    
    $this->_unixtime = strtotime( "+$month month", $this->_unixtime );
    return $this; 
  }
  
  public function setDay( $day )
  {
    $day -= date( 'j', $this->_unixtime );
    
    $this->_unixtime = strtotime( "+$day days", $this->_unixtime );
    return $this; 
  }
  
  public function setYear( $year )
  {
    $year -= date( 'Y', $this->_unixtime ); 
    
    $this->_unixtime = strtotime( "+$year years", $this->_unixtime );
    return $this; 
  }
  
  public function getMonth()
  {
    return date( 'n', $this->_unixtime );
  }
  
  public function getDay()
  {
    return date( 'j', $this->_unixtime );
  }
  
  public function getYear()
  {
    return date( 'Y', $this->_unixtime );
  }
  
  public function getHour()
  {
    return date( 'G', $this->_unixtime );
  }
  
  public function getMinutes()
  {
    return date( 'i', $this->_unixtime );
  }
  
  public function getSeconds()
  {
    return date( 's', $this->_unixtime );
  }
  
  public function show( $format = 'j M Y H:i' )
  {
    return date( $format, $this->_unixtime );
  }
  
  public function getMysqlTimestamp()
  {
    return $this->show( 'Y-m-d H:i:s' );
  }
}

?>